package com.atguigu.filter;

import com.atguigu.bean.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
       //对登录进行过滤
        //进行判断是否已经登录（已经登录的进行方放行，未登录的跳转到登录页面）
        //
        // 获取session  session.userlong.username==null
        HttpServletRequest request =(HttpServletRequest)req;
        HttpServletResponse response =(HttpServletResponse)resp;
        HttpSession session = request.getSession();
        User userLogin =(User)session.getAttribute("userlogin");
        if(userLogin==null){
            //进行重定向
            response.sendRedirect(request.getContextPath()+"/user?flag=tologin");
        }else {
            chain.doFilter(req, resp);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}